Begin A1_DAT_SRT_Exterior


Short TentEnterState
Short TentSavePos
Short TentStrikeState
Short GoodSpot
Short InFront

Float TentDelTimer
Float TentNoCigarTimer
Float TentStrikeTimer
Float Distance
Float AnglePlayer
Float PosXPlayer
Float PosYPlayer
Float PosZPlayer
Float DX
Float DY
Float DZ
Float PosZ


If ( GetDisabled == 1 )
	Set TentDelTimer To ( TentDelTimer + GetSecondsPassed )
	If ( TentDelTimer > .2 )
		Set TentDelTimer To 0
		SetDelete 1
	EndIf
EndIf


If ( TentSavePos == 0 )
	Set A1_DAT_SRT_TentPosX To ( GetPos, X )
	Set A1_DAT_SRT_TentPosY To ( GetPos, Y )
	Set A1_DAT_SRT_TentPosZ To ( GetPos, Z )
	Set TentSavePos To 1
EndIf

If ( OnActivate == 1 )
	If ( GetPCSneaking == 1 )
		MessageBox "Striking Tent"
		Set TentStrikeState to 1
	Else
		MessageBox "You enter the Tent"
		Set TentEnterState to 1
	EndIf
EndIf

If ( TentEnterState == 1 )
	PlaySound, "Item Clothes Down"
	Player -> PositionCell, 1, -98, 3, 0 "Sea Rover's Tent"
	Set TentEnterState To 0
EndIf

If ( TentStrikeState == 1 )
	Player -> AddItem, "A1_DAT_SRT_Pack", 1
	PlaySound, "Item Misc Up"
	Set A1_DAT_SRT_TentStrike to 1
	Set TentStrikeState To 2
	Set A1_DAT_SRT_TentPitched to 0
EndIf

If ( TentStrikeState == 2 )
	Set TentStrikeTimer To ( TentStrikeTimer + GetSecondsPassed )
If ( TentStrikeTimer > .2 )
	Set TentStrikeState To 0
		Disable
	EndIf
EndIf

If ( A1_DAT_SRT_TentPitched == 1 )
	Set InFront to 0
	Set AnglePlayer to ( Player->GetAngle, Z )
	Set PosXPlayer  to ( Player->GetPos, X )
	Set PosYPlayer  to ( Player->GetPos, Y )
	Set PosZPlayer  to ( Player->GetPos, Z )

	Set DX to ( GetPos, X )
	Set DX to ( DX - PosXPlayer )
	Set DY to ( GetPos, Y )
	Set DY to ( DY - PosYPLayer )
	Set DZ to ( GetPos, Z )
	Set DZ to ( DZ - PosZPlayer )
  
If ( DY >= 0 )
	If ( AnglePlayer >= -45 )
		If ( AnglePlayer <= 45 )
		Set InFront to 1
		EndIf
	EndIf
EndIf

If ( DX >= 0 )
	If ( AnglePlayer >= 45 )
		If ( AnglePlayer <= 135 )
			Set InFront to 1
		EndIf
	EndIf
EndIf

If ( DY <= 0 )
	If ( AnglePlayer >= 135 )
		Set InFront to 1
	ElseIf ( AnglePlayer <= -135 )
		Set InFront to 1
	EndIf
EndIf

If ( DX <= 0 )
	If ( AnglePlayer <= 0 )
		Set InFront to 1
	EndIf
EndIf

  ; determine If this is a good spot
Set GoodSpot to -1 ; not an even area
	If ( DZ >= -32 )
	If ( DZ <= 32 )
	Set GoodSpot to 0 ; not an open area
	Set Distance to ( GetDistance, Player )
	If ( Distance <= 768 )
		If ( Distance > 320 )
		Set GoodSpot to InFront ; in front?
			EndIf
		EndIf
	EndIf
EndIf
   ;MessageBox "Tent placement: Distance: %g, in front: %g, DZ: %g", Distance, InFront, DZ 

If ( GoodSpot <= 0 )
	; not a good spot
	Set TentStrikeState to 1
	If ( GoodSpot < 0 )
		MessageBox "This is not a good spot to pitch your tent. Find a more even area."
	Else
      	MessageBox "This is not a good spot to pitch your tent. Find a more open area."
		EndIf
	EndIf
EndIf

End